# -*- coding: utf-8 -*-
import scrapy
import json
import re


class CategorySpider(scrapy.Spider):
    name = 'category'

    def start_requests(self):
        self.start_urls = ['https://dc.3.cn/category/get?&callback=getCategoryCallback']
        for url in self.start_urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        p = str(response.body.decode('gbk'))
        b = p.strip('getCatego  ryCallback(')[:-1]
        result = json.loads(b)
        for i in range(0, 18):
            if i == 12:
                continue
            if i == 14:
                continue
            if i == 15:
                continue
            if i == 16:
                continue
            cat_top_name = result['data'][i]['s'][0]['n']
            cat_top_name = re.findall('\|(.*?)\|\|', cat_top_name)[0]
            # print('一级分类:', cat_top_name)

            second_cat_list_len = len(result['data'][i]['s'][0]['s'])
            for j in range(0, second_cat_list_len):
                cat_second_name = result['data'][i]['s'][0]['s'][j]['n']
                cat_second_name = re.findall('\|(.*?)\|\|', cat_second_name)[0]
                # print('二级分类:', cat_second_name)

                last_cat_list_len = len(result['data'][i]['s'][0]['s'][j]['s'])
                for l in range(0, last_cat_list_len):
                    old_url = result['data'][i]['s'][0]['s'][j]['s'][l]['n']
                    category = re.findall('\|(.*?)\|\|', old_url)[0]
                    # print('三级分类:', category)

                    # 三级分类链接
                    url = re.findall('(.*?)\|', old_url)[0]
                    url = '@' + url
                    o = re.findall('@\d+-\d+-\d+', url)
                    o2 = re.findall('@\d+-\d+-\d+ ', url)
                    o1 = re.findall('@\d+-\d+', url)
                    for j2 in o:
                        if url == j2:
                            url = '@list.jd.com/list.html?cat=' + url.replace('@', '').replace('-', ',')

                    for j3 in o1:
                        if url == j3:
                            url = '@channel.jd.com/' + url.replace('@', '') + '.html'

                    for j4 in o2:
                        if url == j4:
                            url = '@list.jd.com/list.html?cat=' + url.replace('@', '').replace('-', ',')

                    item = {}
                    item['cat_top_name'] = cat_top_name
                    item['cat_second_name'] = cat_second_name
                    item['category'] = category
                    yield item



